//
//  ContaceNewViewController.m
//  BGM
//
//  Created by 我要变好看 on 2019/1/1.
//  Copyright © 2019 com.alirtc.demo. All rights reserved.
//

#import "ContaceNewViewController.h"
#import "Common.h"
#import "BGMUser.h"
#import "BGMHTTP.h"
#import "MessageTableViewCell.h"

@interface ContaceNewViewController ()<

UITableViewDelegate,UITableViewDataSource,
MessageTableViewCellDelegate

>
@property (nonatomic, strong)UITableView *addTable;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, assign)int currentPage;


@end

@implementation ContaceNewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    CGRect rect = self.titleBtn.frame;
    rect.size.width = rect.size.width+70;
    self.titleBtn.frame = rect;
    [self.titleBtn setTitle:@"新的朋友" forState:UIControlStateNormal];
    self.dataArray = [[NSMutableArray alloc]init];
    self.currentPage = 1;
    
    
    self.addTable = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navview.frame.size.height,SCREENW , SCREENH-self.navview.frame.size.height)];
    self.addTable.delegate = self;
    self.addTable.dataSource = self;
    self.addTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.addTable];
    
    [self getFriendRequest];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     return 70;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"MessageTableViewCell";
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell ==nil) {
        cell = [[MessageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.msgDelegate = self;
    }
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    cell.addBtn.tag = indexPath.row;
    [cell newfriendAdd:dic];
    return cell;
}
- (void)getFriendRequest{
    NSDictionary *param = @{@"userToken":[BGMUser shareInstance].userToken,
                            @"usersId":[BGMUser shareInstance].userId,
                            @"pageNum":[NSString stringWithFormat:@"%d",self.currentPage],
                            @"pageSize":@"10"
                            };
    [BGMHTTP requestWithMethodName:@"contacts/getNewFriendsRequestList" parameters:param completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        if ([code isEqualToString:@"200"]) {
            NSArray *array = [result objectForKey:@"data"];
            [self.dataArray addObjectsFromArray:array];
            [self.addTable reloadData];
            if (array.count==10) {
                self.currentPage = self.currentPage+1;
            }else{
                self.currentPage = 0;
            }
        }
    }];
    
    
}

-(void)clickAddFriend:(int)i{
    NSDictionary *dic = [self.dataArray objectAtIndex:i];
    NSMutableDictionary *newDic = [[NSMutableDictionary alloc]initWithDictionary:dic];
    __block int blockI = i;
    NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,
                             @"userId":[BGMUser shareInstance].userId,
                             @"uafId":[NSString stringWithFormat:@"%@",[dic objectForKey:@"uafId"]],
                             @"friendsId":[NSString stringWithFormat:@"%@",[dic objectForKey:@"friendsId"]]
                             };

    [BGMHTTP requestWithMethodName:@"contacts/agreeAddFriends" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        if ([code isEqualToString:@"200"]) {
            [newDic setObject:@"2" forKey:@"state"];
            [self.dataArray replaceObjectAtIndex:i withObject:newDic];
            dispatch_async(dispatch_get_main_queue(), ^{
                NSIndexPath *indexPath=[NSIndexPath indexPathForRow:blockI inSection:0];
                [self.addTable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
                [[NSNotificationCenter defaultCenter]postNotificationName:@"freshContactView" object:nil];
            });
           
        }
    }];
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
